package com.cpcs.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

/**
 * 服务入口类
 */
@Component
public class WsServer {

    private EventLoopGroup mainGroup = null;
    private EventLoopGroup subGroup = null;
    private ServerBootstrap serverBootstrap = null;

    private WsServer() {
        mainGroup = new NioEventLoopGroup();
        subGroup = new NioEventLoopGroup();
        serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(mainGroup, subGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new WSServerInitialzer());
    }

    private static class WsServerSingleton {
        static final WsServer INSTANCE = new WsServer();
    }

    /**
     * 功能描述：WsServer 单例 <br>
     * @author Byron Wang
     * @param
     * @return com.cpcs.netty.WsServer
     * @since 下午10:49 2018/8/23
     */
    public static WsServer getInstance() {
        return WsServerSingleton.INSTANCE;
    }

    /**
     * 功能描述：服务器启动方法 <br>
     * @author Byron Wang
     * @param
     * @return void
     * @since 下午10:49 2018/8/23
     */
    public void start() {
        serverBootstrap.bind(8088);
        System.err.println("Netty Websocket Server Start...");
    }

}
